package 面试;

/*
爱奇艺：
一周股市价格为[2,6,1,4,8]，求哪一天买入哪一天卖出，可获得最大收益，最大收益为多少
 */
public class 股市买卖 {

    public static void main(String[] args) {
        int[] array = {2, 6, 1, 4, 8};
        getMaxProfit(array);
    }

    public static int getMaxProfit(int[] array) {
        int profit = 0;
        int min = array[0];
        int buyDay = 0;
        int sellDay = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
                buyDay = i;
            } else {
                if (array[i] - min > profit) {
                    profit = array[i] - min;
                    sellDay = i;
                }
            }
        }
        System.out.println("第" + (buyDay + 1) + "天买入，第" + (sellDay + 1) + "卖出，可获得最大收益" + profit + "元");
        return profit;
    }
}
